﻿$(document).ready(function () {

    var show_add_license_form = function () {
        $('#AddLicenseForm').clearForm();
        $('#AddLicenseFormContainer').dialog({ title: 'Add a license', width: 500, modal: true });
    };

    $("#list").jqGrid({
        height: 300, 
        width: 1000,
        url: '/License/GetLicensesData/',
        datatype: 'json',
        mtype: 'POST',
        colNames: ['Id', 'Name', 'Summary'],
        colModel: [
          { name: 'Id', index: 'Id', width: 275, sortable: false },
          { name: 'Name', index: 'Name', width: 300, class: 'required', sortable: false },
          { name: 'Summary', index: 'Summary', width: 425, class: 'required', sortable: false }
          ],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [10, 50],
        sortname: 'name',
        sortorder: "asc",
        viewrecords: true,
        imgpath: '/Content/images',
        caption: 'Licenses'
    }).navGrid('#pager', { edit: false, add: false, del: false, search: false }).jqGrid('navButtonAdd', "#pager", { caption: "", buttonicon: "ui-icon-plus", onClickButton: show_add_license_form, position: "first", title: "", cursor: "pointer" });

    $('#AddLicenseForm input[name=Feature]').autocomplete({ source: '/Feature/SearchFeatures', highlight: true, multiple: false, mustMatch: true, minChars: 3, autoFill: true });

    var LicenseClientTypes = ['Checkout', 'Machine', 'Process', 'TerminalServicesSession', 'User'];
    $('#AddLicenseForm input[name=ClientTypes]').autocomplete({ source: ['Checkout', 'Machine', 'Process', 'TerminalServicesSession', 'User'], max: 5, highlight: true, multiple: false, mustMatch: true, minChars: 1, autoFill: true });

    $('#AddLicenseForm a[name=Submit]').click(function () { $('#AddLicenseForm').submit(); });
    $('#AddLicenseForm a[name=Cancel]').click(function () { $("#AddLicenseFormContainer").dialog("close"); });
    $('#AddLicenseForm').validate({
        rules: {
            Limit: {
                digits: true,
                required: {
                    depends: function () {
                        var selectedLicenseType = $('#AddLicenseForm select[name=Type]').val();
                        return selectedLicenseType != 'NodeLockedLicense' && selectedLicenseType != 'UncountedFloatingLicense';
                    }
                }
            },
            ClientTypes: {
                required: {
                    depends: function () {
                        var selectedLicenseType = $('#AddLicenseForm select[name=Type]').val();
                        return selectedLicenseType != 'NodeLockedLicense';
                    }
                }
            }
        }
    });

    $('#AddLicenseForm').ajaxForm({
        success: function (data) {
            if (data && data[0]) {
                $("#AddLicenseFormContainer").dialog("close");
                $('#list').trigger('reloadGrid');
            }
        }
    });

}); 